package com.inossem.warehousestorage_inossem.activity.emergency_warehousing;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.EmergencyWarehousingUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

/**
 * 作者：wsy on 2017/11/28 17:22
 * <p>
 * 邮箱：445979770@qq.com
 */

public class EmergencyWarehousingReceiveMaterielDetailActivity extends BaseActivity {


    private static final String TYPE_SERIAL = "serial";

    private static final String TYPE_INDEX = "index";

    private TextView title, serialNumber, materielCode, materielDescription, number, storageArea, position, state, processingDate, associatedDocuments, dealingPeople , pageInfo;

    private Button previous, next;

    private String serialNumberStr, unit, mMaterielCode;

    private int index;

    private EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean materielItem;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_emergency_warehousing_receive_materiel_detail);
        initView(EmergencyWarehousingReceiveMaterielDetailActivity.this);
        initData(EmergencyWarehousingReceiveMaterielDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        serialNumber = (TextView) activity.findViewById(R.id.serialNumber);
        materielCode = (TextView) activity.findViewById(R.id.materielCode);
        materielDescription = (TextView) activity.findViewById(R.id.materielDescription);
        number = (TextView) activity.findViewById(R.id.number);
        storageArea = (TextView) activity.findViewById(R.id.storageArea);
        position = (TextView) activity.findViewById(R.id.position);
        state = (TextView) activity.findViewById(R.id.state);
        processingDate = (TextView) activity.findViewById(R.id.processingDate);
        associatedDocuments = (TextView) activity.findViewById(R.id.associatedDocuments);
        dealingPeople = (TextView) activity.findViewById(R.id.dealingPeople);

        pageInfo = (TextView) activity.findViewById(R.id.pageInfo);
        previous = (Button) activity.findViewById(R.id.previous);
        next = (Button) activity.findViewById(R.id.next);
    }

    private void initData(BaseActivity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_materiel_detail_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_emergency_warehousing_doverview, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                backAndUpdate(activity);
            }
        });
        previous.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (index > 0) {
                    index--;
                    getData(activity, TYPE_INDEX);
                } else {
                    backAndUpdate(activity);
                }
            }
        });
        next.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (index < (EmergencyWarehousingUtils.getReceiveMaterielListSize(activity) - 1)) {
                    index++;
                    getData(activity, TYPE_INDEX);
                } else {
                    backAndUpdate(activity);
                }
            }
        });
        getExtra(activity);
        getIndexBySerial(activity, serialNumberStr);
        getData(activity, TYPE_SERIAL);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            int indexTemp = InStorageUtils.getReceiveIndexByMaterielCode(activity, mMaterielCode);
            if (indexTemp != -1 && indexTemp != index) {
                index = indexTemp;
                getData(activity, TYPE_INDEX);
            }
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private void getExtra(Activity activity) {
        serialNumberStr = activity.getIntent().getStringExtra(InStorageExtra.SERIAL_NUMBER);
    }

    private void getIndexBySerial(Context context, String serialNumber) {
        index = EmergencyWarehousingUtils.getReceiveIndexBySerial(context, serialNumber);
    }

    private void getData(BaseActivity context, String getMaterielInformationFromType) {
        switch (getMaterielInformationFromType) {
            case TYPE_SERIAL:
                materielItem = EmergencyWarehousingUtils.getReceiveMaterielInformationBySerial(context, serialNumberStr);
                LogUtils.i("materielItem === " + materielItem);
                break;
            case TYPE_INDEX:
                materielItem = EmergencyWarehousingUtils.getReceiveMaterielInformationByIndex(context, index);
                break;
        }
        unit = materielItem.getUnit_code();
        serialNumber.setText(materielItem.getUrgence_rid());
        materielCode.setText(materielItem.getMat_code());
        materielDescription.setText(materielItem.getMat_name());
        number.setText(materielItem.getQty() + unit);
        storageArea.setText(materielItem.getArea_code());
        position.setText(materielItem.getPosition_id()+ "-" + materielItem.getPosition_code());
        state.setText(materielItem.getStatus());
        processingDate.setText(materielItem.getOperation_time());
        associatedDocuments.setText(materielItem.getRelate_receipt_code());
        dealingPeople.setText(materielItem.getUser_name());
        pageInfo.setText(EmergencyWarehousingUtils.getReceiveMaterielListSize(context) + " | " + (index + 1));
        if (index <= 0) {
            previous.setText(R.string.activity_receive_result_back);
        } else {
            previous.setText(R.string.activity_in_storage_receive_materiel_detail_previous);
        }
        if (index >= (EmergencyWarehousingUtils.getReceiveMaterielListSize(context) - 1)) {
            next.setText(R.string.activity_receive_result_back);
        } else {
            next.setText(R.string.activity_in_storage_receive_materiel_detail_next);
        }
    }


    private void backAndUpdate(Context context) {
        super.onBackPressed();
    }

    @Override
    public void onBackPressed() {
        backAndUpdate(EmergencyWarehousingReceiveMaterielDetailActivity.this);
    }
}
